<?php
namespace app\dc\controller;


use think\Controller;
use think\Db;

class  Login extends Controller{
    public function loginPost()
    {
        if ($this->request->isAjax()) {
            $data = input('');
            // dump($data);
            $mobile = trim($data['account']);
            $password = trim($data['password']);
            if (!$mobile || !$password) {
                $this->error('数据异常');
            }

            $user = Db::name('dc')->where('mobile', $mobile)->find();
            if (!$user || $user['status'] != 1) {
                $this->error('用户不存在 或状态异常');
            }
            if ($user['password'] != cmf_password($password)) {
                $this->error('验证失败');
            }
            session('dc_id', $user['id']);
            session('admin_info', $user);
            $updata = [];
            $updata['logintime'] = time();
//            $updata['last_ip'] = request()->ip();

            Db::name('dc')->where('id', $user['id'])->update($updata);


            $this->success('验证通过', '', ['access_token' => request()->token()]);
        }

        return $this->fetch();
    }

    public function loginPost1()
    {
        $data = input();
        $account = $data['account'];
        $password = $data['password'];
        if ($account && $password) {
            $dcTable = Db::name('dc');
            $dc = $dcTable->where('mobile', $account)->find();

            if (!$dc) {
                $this->error('您输入的帐号或者密码不正确，请重新输入。');
            }

            if ($dc['password'] != md5(md5($password) . $dc['salt'])) {
                $dcTable->where('name', $account)
                    ->update(array('loginfailure' => $dc['loginfailure']++));
                $this->error('您输入的帐号或者密码不正确，请重新输入。');
            }

            $dcTable->where('name', $account)
                ->update(array('loginfailure' => $dc['loginfailure']++, 'loginfailure' => 0, 'logintime' => time(), 'loginip' => $this->request->ip()));
            session("dc", $dc);
            $this->success();
        }
        $this->error('您输入的帐号或者密码不正确，请重新输入。');
    }

    public function logout()
    {
//        session('dc', null);
//        $this->redirect(url('/index/index/index'));


        session('dc_id', null);
        session('dc_info', null);
        $this->redirect(url('/index/index/index'));
    }


}